{extend name="public/container"}
{block name="content"}
<style type="text/css">
    .order-price span {
        font-size: 12px;
        color: #999;
    }
    .order-price span:not(:last-child) {
        margin-right: 20px;
    }
    .order-price span b {
        color: #FF0000;
        font-size: 17px;
        margin-right: 2px;
    }
</style>
<div id="myApp" v-cloak>
    <div class="douxue-filter padding-20 flex flex-between" style="margin-bottom:20px;">
        <el-form :inline="true" :model="where" size="mini" class="flex">
            <div class="flex flex-between">
                <el-form-item>
                    <el-input v-model="where.title" placeholder="代金券标题">
                        <el-button slot="append" size="small" type="success" @click="doLoadRows(1)" icon="el-icon-search"></el-button>
                    </el-input>
                </el-form-item>
            </div>
        </el-form>
        <div>
            <el-button type="primary" size="mini" @click="doAdd()">新增代金券</el-button>
            <el-button type="success" size="mini" @click="goRules()">使用规则</el-button>
        </div>
    </div>

    <div class="padding-10">
        <el-table
            :data="rows"
            style="width: 100%"
            v-loading="loading"
            element-loading-text="加载中"
            element-loading-spinner="el-icon-loading"
            size="mini">
            <el-table-column
                prop="title"
                label="代金券标题"
                align="left">
            </el-table-column>

            <el-table-column
                prop="coupon_value"
                label="面值"
                width="120"
                align="left">
            </el-table-column>

            <el-table-column
                prop="issue_nums"
                label="发行数量"
                width="120"
                align="left">
            </el-table-column>

            <el-table-column
                prop="expire_type_display"
                label="过期类型"
                width="120"
                align="left">
            </el-table-column>

            <el-table-column
                prop="expire_value_display"
                label="过期时间"
                width="160"
                align="left">
            </el-table-column>

            <el-table-column
                prop="use_rule_rtitle"
                label="使用规则"
                width="120"
                align="left">
            </el-table-column>

            <el-table-column
                prop="use_rule_rtitle"
                label="领取记录"
                width="120"
                align="left">
                <template slot-scope="scope">
                    <el-button icon="el-icon-s-marketing" size="mini" type="danger" @click="viewCouponRecords(scope.row)">查看</el-button>
                </template>
            </el-table-column>

            <el-table-column
                prop="status"
                label="是否启用"
                width="120"
                align="left">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        active-color="#ff6a6c"
                        inactive-color="#999"
                        :active-value="1"
                        :inactive-value="0"
                        @change="doSetStatus(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>

            <el-table-column
                label="操作"
                fixed="right"
                width="90"
                align="left">
                <template slot-scope="scope">
                    <el-link type="primary" size="mini" :underline="false" @click="doAdd(scope.row)">编辑</el-link>
                    <el-link type="danger" size="mini" :underline="false" @click="doDelete(scope.row.id)">删除</el-link>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                small
                :hide-on-single-page="true"
                layout="prev, pager, next"
                :total="rowsCount"
                :page-size="where.limit"
                @current-change="doLoadRows">
            </el-pagination>
        </div>
    </div>

    <!-- 添加/编辑   -->
    <el-dialog title="添加/编辑" v-if="dialogFormVisible" :visible.sync="dialogFormVisible" @close='closeDialog'>
        <el-form :model="formData" :rules="rules" ref="formData" label-width="100px" >
            <el-form-item label="代金券标题" prop="title">
                <el-input v-model="formData.title" placeholder="代金券标题" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="面额" prop="coupon_value">
                <el-input type="number" v-model="formData.coupon_value" placeholder="面额" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="发行数量" prop="issue_nums">
                <el-input type="number" v-model="formData.issue_nums" placeholder="发行数量" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="过期方式" prop="expire_type">
                <el-select
                    size="small"
                    v-model="formData.expire_type"
                    placeholder="过期方式"
                    :disabled="formData.id > 0"
                    :clearable=true>
                    <el-option label="固定日期" :value="1"></el-option>
                    <el-option label="领取X天后" :value="2"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="过期时间" prop="expire_type" v-if="formData.expire_type > 0">
                <template v-if="formData.expire_type==1">
                    <el-date-picker
                        v-model="formData.expire_value"
                        type="date"
                        value-format="yyyy-MM-dd"
                        :disabled="formData.id > 0"
                        placeholder="选择日期">
                    </el-date-picker>
                </template>
                <template v-if="formData.expire_type==2">
                    <el-input type="number" v-model="formData.expire_value" :disabled="formData.id > 0" placeholder="请填写天数" autocomplete="off">
                        <template slot="prepend">领取后</template>
                        <template slot="append">天过期</template>
                    </el-input>
                </template>
            </el-form-item>

            <el-form-item label="使用规则" prop="use_rule_id">
                <el-select
                    size="small"
                    v-model="formData.use_rule_id"
                    placeholder="使用规则"
                    :clearable=true>
                    <el-option :label="item.rtitle" :value="item.id" v-for="item in useRules"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="是否启用">
                <el-select size="small" placeholder="是否启用" v-model="formData.status">
                    <el-option label="否" :value="0"></el-option>
                    <el-option label="是" :value="1"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit('formData')">确 定</el-button>
                <el-button @click="dialogFormVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <!-- 优惠券领取记录   -->
    <el-drawer
        title="领取记录"
        :visible.sync="showCouponRecords"
        direction="rtl"
        size="80%"
        >
        <div style="padding: 0 20px;">
            <div class="records-header">
                <el-descriptions class="margin-top" title="代金券信息" :column="3" border>
                    <el-descriptions-item>
                      <template slot="label">
                        <i class="el-icon-user"></i>
                        名称
                      </template>
                      {{ couponRecords.coupon.title }}
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template slot="label">
                        <i class="el-icon-mobile-phone"></i>
                        发行数量
                      </template>
                      {{ couponRecords.coupon.issue_nums }}
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template slot="label">
                        <i class="el-icon-location-outline"></i>
                        面值
                      </template>
                      {{ couponRecords.coupon.coupon_value }}
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template slot="label">
                        <i class="el-icon-tickets"></i>
                        过期类型

                      </template>
                      {{ couponRecords.coupon.expire_type_display }}
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template slot="label">
                        <i class="el-icon-office-building"></i>
                        过期时间
                      </template>
                      {{ couponRecords.coupon.expire_value_display }}
                    </el-descriptions-item>
                  </el-descriptions>
            </div>

            <div class="records-table">
                <div style="font-size:16px; font-weight: bold;margin: 20px 0;">领取记录</div>
                <div class="table-wrap">
                    <el-form :inline="true"  size="mini" class="flex">
                        <div class="flex flex-between">
                            <el-form-item>
                                <el-input v-model="couponRecordsWhere.mobile" placeholder="手机号">
                                    <el-button slot="append" size="small" type="success" @click="doLoadCouponRecordsRows(1)" icon="el-icon-search"></el-button>
                                </el-input>
                            </el-form-item>
                        </div>
                    </el-form>

                    <el-table
                        :data="couponRecords.rows"
                        style="width: 100%"
                        v-loading="couponRecordsLoading"
                        element-loading-text="加载中"
                        element-loading-spinner="el-icon-loading"
                        size="mini">
                        <el-table-column
                            prop="title"
                            label="领取用户"
                            align="left">
                            <template slot-scope="scope">
                                <div>
                                    <el-link :underline="false" type="primary">{{ scope.row.nickname }}（UID：{{ scope.row.uid }}）</el-link>
                                </div>
                                <div>
                                    <el-link :underline="false" type="primary">{{ scope.row.mobile }}</el-link>
                                </div>
                            </template>
                        </el-table-column>

                        <el-table-column
                            prop="obtain_type_display"
                            label="获取方式"
                            width="100"
                            align="left">
                        </el-table-column>

                        <el-table-column
                            prop="obtain_time"
                            label="领取时间"
                            width="160"
                            align="left">
                        </el-table-column>

                        <el-table-column
                            prop="expire_time"
                            label="过期时间"
                            width="160"
                            align="left">
                        </el-table-column>

                        <el-table-column
                            prop="status_display"
                            label="领取状态"
                            width="120"
                            align="left">
                            <template slot-scope="scope">
                                <el-tag size="mini" type="success" v-if="scope.row.status==1">{{ scope.row.status_display }}</el-tag>
                                <el-tag size="mini" type="info" v-if="scope.row.status==2">{{ scope.row.status_display }}</el-tag>
                                <el-tag size="mini" type="danger" v-if="scope.row.status==3">{{ scope.row.status_display }}</el-tag>
                                <el-tag size="mini" type="warning" v-if="scope.row.status==4">{{ scope.row.status_display }}</el-tag>
                            </template>
                        </el-table-column>

                        <el-table-column
                            prop="use_time_display"
                            label="使用状态"
                            width="160"
                            align="left">
                            <template slot-scope="scope">
                                <div>
                                    <el-link :underline="false" type="primary">时间：{{ scope.row.use_time_display }}</el-link>
                                </div>
                                <div>
                                    <el-link :underline="false" type="danger">核销：{{ scope.row.use_type_display }}</el-link>
                                </div>
                            </template>
                        </el-table-column>

                        <el-table-column
                            prop="use_time_display"
                            label="操作"
                            width="80"
                            fixed="right"
                            align="right">
                            <template slot-scope="scope">
                                <div>
                                    <el-link :underline="false" type="warning" v-if="scope.row.use_type==0" @click="verifyCoupon(scope.row.id)">核销</el-link>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination">
                        <el-pagination
                            small
                            :hide-on-single-page="true"
                            layout="prev, pager, next"
                            :total="couponRecords.rowsCount"
                            :page-size="couponRecordsWhere.limit"
                            @current-change="doLoadCouponRecordsRows">
                        </el-pagination>
                    </div>
                </div>
            </div>
        </div>
    </el-drawer>
</div>
{/block}
{block name="script"}
<script>
    var _formData = {
        id:0,
        title: '',
        coupon_value: 0,
        issue_nums: 0,
        use_rule_id: '',
        status: 1,
        expire_type: 1,
        expire_value: ''
    }

    var my = new Vue({
        el: "#myApp",
        data(){
            return {
                dialogFormVisible: false,
                formData:_formData,
                rules:{
                    title: [
                        { required: true, message: '此项不能为空', trigger: 'blur' },
                    ],
                    coupon_value: [
                        { required: true, message: '此项不能为空', trigger: 'change' },
                    ],
                    issue_nums: [
                        { required: true, message: '此项不能为空', trigger: 'change' },
                    ],
                    expire_type: [
                        { required: true, message: '此项不能为空', trigger: 'change' },
                    ],
                    expire_value: [
                        { required: true, message: '此项不能为空', trigger: 'change' },
                    ]
                },
                rowsCount: 0,
                rows: [],
                where: {
                    keyword: '',
                    stype:'',
                    date_time:'',
                    page:1,
                    limit:pageSize,

                },
				useRules: [],
                loading:false,
                showCouponRecords: false,
                couponRecordsLoading: false,
                couponRecords: {
                    coupon: {},
                    rows: [],
                    rowsCount: 0,
                },
                couponRecordsWhere: {
                    coupon_id:'',
                    mobile: '',
                    page: 1,
                    limit: pageSize,
                },
            }
        },
        methods: {
            doLoadRows (page) {
                this.loading = true
                this.where.page  = page || 1
                axios.get(Url({a:'get_list' , q:this.where}))
                .then(res => {
                    this.rows = res.data.data
                    this.rowsCount =res.data.count
                    this.loading = false
                })
            }
			// 设置状态
            ,doSetStatus (row) {
                this.$confirm('状态更改后立即生效。确认操作吗?').then(_ => {
                    axios.get(Url({a:'set_status',q:{status:row.status,id:row.id}})).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                            onClose: _ => {
                                if (res.data.code !== 0) {
                                    this.doLoadRows()
                                }
                            }
                        })
                    })
                }).catch(_ => {})
            }
			// 编辑
            ,doAdd ( item ) {
                if ( item != undefined ) {
                    this.formData = item;
                } else {
                    this.formData = _formData
                }
                this.dialogFormVisible = true
            }
            ,onSubmit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post( Url({a:'save'}) , this.formData)
                            .then(res => {
                                this.$message({
                                    message:'操作成功',
                                    type:'success',
                                    duration:1500
                                });
                                this.dialogFormVisible = false;
                                this.doLoadRows(1)
                            }).catch(_ => {
                            this.$message({
                                message: '操作失败',
                                type: 'error'
                            })
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
            ,doDelete (id) {
                this.$confirm('确认删除该条记录?').then(_ => {
                    axios.get(Url({a:'delete' , q:{id:id}})).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                        })
                    }).catch(_ => {
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        })
                    })

                    return this.doLoadRows();
                }).catch(_ => {})
            }
            // 关闭
            ,closeDialog () {
                this.$refs['formData'].clearValidate();
            }
            ,loadCouponRules() {
                axios.get(Url({c:'finance.coupon_rules',a:'get_all'}))
                .then(res => {
                    this.useRules = res.data.data
                })
            }
            ,goRules() {
                window.location.href = Url({c:'finance.coupon_rules',a:'index'})
            }
            ,viewCouponRecords(coupon) {
                this.showCouponRecords = true
                this.couponRecords.coupon = coupon
                this.couponRecordsWhere.coupon_id = coupon.id || 0
                this.doLoadCouponRecordsRows()
            }
            ,doLoadCouponRecordsRows(page) {
                this.couponRecordsLoading = true
                this.couponRecordsWhere.page  = page || 1
                axios.get(Url({c:'finance.coupon_records',a:'get_list',q:this.couponRecordsWhere}))
                .then(res => {
                    this.couponRecords.rows = res.data.data
                    this.couponRecords.rowsCount = res.data.count
                    this.couponRecordsLoading = false
                })
            }
            ,verifyCoupon(recId) {
                this.$confirm('此操作不可撤销，确认核销该券吗?').then(_ => {
                    axios.post(Url({c:'finance.coupon_records', a:'verifyCoupon'}) , {recId:recId}).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                        })
                    }).catch(_ => {
                        this.$message({
                            message: '核销失败',
                            type: 'error'
                        })
                    })

                    return this.doLoadCouponRecordsRows();
                }).catch(_ => {})
            }
        },
        mounted(){
            this.doLoadRows()
			this.loadCouponRules()
        }
    })
</script>
{/block}